from typing import List

from sortedcontainers import SortedList


class FindSumPairs:

    def __init__(self, nums1: List[int], nums2: List[int]):
        self.nums1 = nums1
        self.nums2 = nums2
        self.sorted_nums2 = SortedList(nums2)

    def add(self, index: int, val: int) -> None:
        self.sorted_nums2.remove(self.nums2[index])
        self.nums2[index] += val
        self.sorted_nums2.add(self.nums2[index])

    def count(self, tot: int) -> int:
        ans = 0
        for num in self.nums1:
            aim = tot - num
            l = self.sorted_nums2.bisect_left(aim)
            r = self.sorted_nums2.bisect_right(aim)
            ans += r - l
        return ans


if __name__ == "__main__":
    obj = FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4])
    print(obj.count(7))  # 8
    obj.add(3, 2)
    print(obj.count(8))  # 2
    print(obj.count(4))  # 1
    obj.add(0, 1)
    obj.add(1, 1)
    print(obj.count(7))  # 11
